package com.ymatou.iapi.liveseller.testcase;

import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.SetLiveProductTopBean;
import com.ymatou.iapi.liveseller.service.SetLiveProductTopCall;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.livevideo.parameter.StreamLiveNotifyBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*商品置顶
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SetLiveProductTop{
	private static SetLiveProductTopBean setliveproducttopBean;
	private static SetLiveProductTopCall setliveproducttopCall;
	private static LiveRoomWapper liveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品置顶");
	}

	@Before
	public void caseUp() {
		setliveproducttopBean=new SetLiveProductTopBean();
		setliveproducttopCall=new SetLiveProductTopCall();
		liveRoomWapper=new LiveRoomWapper();
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setliveproducttopCall.getOpurl());
		liveRoomWapper=null;
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetLiveProductTop_001() {
		Logger.start(true,"status0未开始商品置顶失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			
			setliveproducttopBean.setLiveId(createlive.getId());
			setliveproducttopBean.setProductId(pid);
			setliveproducttopCall.setData(setliveproducttopBean);
			setliveproducttopCall.callService();			
			Logger.verifyEquals("201", setliveproducttopCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播未开始，无法置顶直播", setliveproducttopCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SetLiveProductTop_002() {
		Logger.start(true,"不输入liveid失败");
		try {
//			List<Userinfo> list = new JUserWapper().select1KSeller();
//			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
//			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
//			
//			Logger.comment("创建商品：" + pid);
//			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
//			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
//			new LiveRoomWapper().updateLiveRoomTime(YMTDateUtil.getBeforeOrNextMunite(-5), null, createlive.getId());
			
//			setliveproducttopBean.setLiveId(createlive.getId());
			setliveproducttopBean.setProductId(TestDataManager.getRandomUUID().toString());
			setliveproducttopCall.setData(setliveproducttopBean);
			setliveproducttopCall.callService();			
			Logger.verifyEquals("400", setliveproducttopCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播Id参数错误", setliveproducttopCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SetLiveProductTop_003() {
		Logger.start(true,"不输入ProductId失败");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
//			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			setliveproducttopBean.setLiveId(createlive.getId());
//			setliveproducttopBean.setProductId(TestDataManager.getRandomUUID().toString());
			setliveproducttopCall.setData(setliveproducttopBean);
			setliveproducttopCall.callService();			
			Logger.verifyEquals("400", setliveproducttopCall.getString("Code"), "验证Code");
			Logger.verifyEquals("商品Id不能为空", setliveproducttopCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SetLiveProductTop_004() {
		Logger.start(true,"输入错误ProductId,不会设置");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			setliveproducttopBean.setLiveId(createlive.getId());
			setliveproducttopBean.setProductId(pid+1);
			setliveproducttopCall.setData(setliveproducttopBean);
			setliveproducttopCall.callService();			
			Logger.verifyEquals("200", setliveproducttopCall.getString("Code"), "验证Code");
			List<Map> top = liveRoomWapper.selectTopLiveproductByLiveid(createlive.getId());
			Logger.debug(top);
			Logger.verifyEquals(0,top.size(), "验证没有top");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SetLiveProductTop_005() {
		Logger.start(true,"status2已经推流直播商品置顶成功");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			setliveproducttopBean.setLiveId(createlive.getId());
			setliveproducttopBean.setProductId(pid);
			setliveproducttopCall.setData(setliveproducttopBean);
			setliveproducttopCall.callService();			
			Logger.verifyEquals("200", setliveproducttopCall.getString("Code"), "验证Code");
			List<Map> top = liveRoomWapper.selectTopLiveproductByLiveid(createlive.getId());
			Logger.debug(top);
			Logger.verifyEquals(1,top.size(), "验证只有一个top");
			Logger.verifyEquals(pid,top.get(0).get("ProductId"), "验证pid");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SetLiveProductTop_006() {
		Logger.start(true,"status3已经断流直播商品置顶成功");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			
			StreamLiveNotifyBean streamlivenotifyBean = LiveRoomCallService.initStreamLiveNotifyBean(createlive.getId(), 0);
			LiveRoomCallService.LiveNotify(streamlivenotifyBean);
			
			setliveproducttopBean.setLiveId(createlive.getId());
			setliveproducttopBean.setProductId(pid);
			setliveproducttopCall.setData(setliveproducttopBean);
			setliveproducttopCall.callService();			
			Logger.verifyEquals("200", setliveproducttopCall.getString("Code"), "验证Code");
			List<Map> top = liveRoomWapper.selectTopLiveproductByLiveid(createlive.getId());
			Logger.debug(top);
			Logger.verifyEquals(1,top.size(), "验证只有一个top");
			Logger.verifyEquals(pid,top.get(0).get("ProductId"), "验证pid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	public void Tc_SetLiveProductTop_007() {
		Logger.start(true,"status7系统自动关闭置顶失败");
		try {
			//此处关闭时候会把所有商品设置成暂停销售
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 7);
			setliveproducttopBean.setLiveId(createlive.getId());
			setliveproducttopBean.setProductId(pid);
			setliveproducttopCall.setData(setliveproducttopBean);
			setliveproducttopCall.callService();			
			Logger.verifyEquals("200", setliveproducttopCall.getString("Code"), "验证Code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	public void Tc_SetLiveProductTop_008() {
		Logger.start(true,"status8未推流关闭直播置顶失败");
		try {
			//此处关闭时候会把所有商品设置成暂停销售
			org.junit.Assert.fail();
			setliveproducttopCall.setData(setliveproducttopBean);
			setliveproducttopCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	public void Tc_SetLiveProductTop_009() {
		Logger.start(true,"status9已推流结束直播置顶失败");
		try {
			//此处关闭时候会把所有商品设置成暂停销售
			org.junit.Assert.fail();
			setliveproducttopCall.setData(setliveproducttopBean);
			setliveproducttopCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	public void Tc_SetLiveProductTop_010() {
		Logger.start(true,"status10运营强制关停排序失败");
		try {
			//此处关闭时候会把所有商品设置成暂停销售
			org.junit.Assert.fail();
			setliveproducttopCall.setData(setliveproducttopBean);
			setliveproducttopCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetLiveProductTop_011() {
		Logger.start(true,"status1待开始商品置顶失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			setliveproducttopBean.setLiveId(createlive.getId());
			setliveproducttopBean.setProductId(pid);
			setliveproducttopCall.setData(setliveproducttopBean);
			setliveproducttopCall.callService();			
			Logger.verifyEquals("201", setliveproducttopCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播未开始，无法置顶直播", setliveproducttopCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}